1
Easy2Siksha
GNDU Question Paper – 2023
Bachelor of Computer Application (BCA) 2nd Semester
INTRODUCTION TO PROGRAMMING IN-C++
Time Allowed – 3 Hours Maximum Marks-75
Note: Attempt FIVE questions in all, selecting at least ONE question from each section. The
fifth question may be attempted from any section. All questions carry equal marks.
SECTION-A
1. What is Object Oriented Programming? How will you compare it with structured
Programming?
2. What is meant by data hiding and encapsulation?
SECTION-B
3. Write and explain a Program in C++ that uses a constructor and destructor.
4. How will you overload a constructor in C++?
SECTION-C
5. Explain the concept of function overloading using C++ code.
6. Overload any one unary operator available in C++.
SECTION-D
7. What are the different types of inheritances? Explain with C++ code.
8. What is class template ? Explain using C++.
2
Easy2Siksha
GNDU Answer Paper 2023
Bachelor of Computer Application (BCA) 2nd Semester
INTRODUCTION TO PROGRAMMING IN-C++
SECTION-A
1.What is Object Oriented Programming? How will you compare it with structured
Programming?
Ans: Object-Oriented Programming (OOP):
Object-Oriented Programming (OOP) is a way of designing and organizing code based on the
concept of "objects." An object is like a self-contained building block that holds both data
and the functions that operate on that data. Think of it as a mini-program that can do things
and store information. In OOP, these objects are created from templates called "classes."
Now, let's break down some key ideas in OOP:
1. Objects: Objects are like little capsules that group together data and the functions
that work with that data. For example, if you have a program about animals, you
might have an object called "Dog" that stores information like the dog's name, age,
and color, along with functions like "bark" or "fetch."
2. Classes: Classes are like blueprints for creating objects. They define what kind of
data an object will store and what functions it can perform. Going back to our
example, "Dog" would be a class, and when you create a specific dog (let's say,
"Buddy"), you're making an object from that class.
3. Encapsulation: This is like putting your data and functions in a box. You hide the
inner workings of your object and only expose what's necessary. In our "Dog"
example, you might not need to know how the "bark" function actually works; you
just know that calling it makes the dog bark.
4. Inheritance: This is a way of creating new classes that are built upon existing classes.
It's like saying, "I want a new thing that's a lot like this other thing, but with some
extra features." In our animal program, you might have a general "Animal" class and
then create more specific classes like "Dog" or "Cat" that inherit traits from the
general "Animal" class.
5. Polymorphism: This is a fancy word that means "many forms." In OOP, it lets you use
a single interface to represent different types of objects. Going back to our animal
example, you might have a function called "makeSound" that works for both dogs
and cats, even though they make different sounds.
3
Easy2Siksha
Now, let's compare this with Structured Programming:
Structured Programming:
Structured Programming is a simpler way of organizing code. Instead of focusing on objects,
it's more about breaking your program into smaller, more manageable parts.
Procedures/Functions: In Structured Programming, you have procedures or functions.
These are like little blocks of code that do a specific job. For instance, you might have a
function to calculate the average of some numbers.
Modularization: This is about breaking your program into modules or sections. Each module
focuses on a specific task. It's like having different rooms in a house, and each room has a
particular purpose.
Control Structures: Structured Programming uses loops (for, while) and conditional
statements (if-else) to control the flow of your program. It's like having a recipe where you
follow step by step.
Comparison:
Approach to Problem Solving:
➢ OOP thinks about problems in terms of objects and their interactions.
➢ Structured Programming thinks about problems in smaller steps, like following a
recipe.
Code Organization:
➢ OOP organizes code around objects and classes, making it more intuitive for real-
world modeling.
➢ Structured Programming organizes code around functions or procedures, making it
clear and easy to follow.
Code Reusability:
➢ OOP promotes reusing code through features like inheritance, where you can use
existing classes to create new ones.
➢ Structured Programming achieves reusability through modularization, where you can
reuse functions in different parts of your program.
Data Handling:
➢ OOP encapsulates data within objects, controlling access through methods.
➢ Structured Programming uses variables, but the focus is less on encapsulation.
Flexibility and Extensibility:
➢ OOP provides flexibility and extensibility through polymorphism and inheritance,
allowing for more dynamic and adaptable code.
➢ Structured Programming relies on modular design for extensibility, making it
straightforward but perhaps not as flexible as OOP.
4
Easy2Siksha
In a nutshell, OOP is like building with Lego blocks, where each block is a self-contained unit
with a specific purpose. Structured Programming is more like following a recipe, where you
break down the cooking process into clear steps. Both have their strengths, and the choice
often depends on the nature of the project and personal preferences. Many modern
programming languages support both paradigms, giving developers the flexibility to choose
based on the task at hand.
2.What is meant by data hiding and encapsulation?
Ans: Data Hiding Simplified:
Imagine you have a (treasure ) chest. You don't want everyone to see what's inside or
mess with your precious things. So, you hide it and only show the key to a few trusted
friends. In the programming world, this hiding of important details is called data hiding.
In computer programs, we deal with lots of information, or "data." Some data is like the
secret treasure that we don't want just anyone to access or change. Data hiding is the
practice of keeping this important information hidden from prying eyes, allowing only
specific parts of a program to use or modify it.
Think of a computer program as a big party. At the party, different parts of the program
need to interact, but not everyone should mess with every detail. Data hiding is like having
private rooms at the party – some areas are open to everyone (public), some are for specific
friends (protected), and some are strictly off-limits (private).
This helps keep things organized, secure, and prevents accidental interference. So, data
hiding is like putting your treasures in a locked room at the party, allowing only certain
friends to have the key.
Encapsulation Simplified:
• Now, imagine you have a magic box. This magic box can not only store your
treasures but also perform tricks when you ask. This box is like an encapsulated
object in programming.
• In the computer world, we often deal with bundles of information and actions, just
like this magic box. Encapsulation is the concept of putting these related things
together in a neat package. So, instead of scattering them all around, we keep them
in one organized unit – like a box that does tricks.
• In simpler terms, encapsulation is like having a lunchbox with compartments. Each
compartment holds a different part of your lunch (data), and the lunchbox itself has
a handle (methods) to carry it around.
• Now, the cool part about this lunchbox is that you don't need to know how each
food item is made or how the lunchbox opens. You just need to know how to use the
handle and open the box. In programming, this is akin to hiding the complicated
details inside the box and only exposing what's necessary.
5
Easy2Siksha
• So, encapsulation is like putting related stuff in a lunchbox, making it easy to carry
around and use without worrying about what's happening inside. It helps keep
things organized and makes your program more user-friendly.
Putting It Together:
➢ Imagine you have a diary with a lock. The diary holds your secrets, and the lock
keeps them safe from prying eyes. This is a simple example of data hiding. You hide
your private thoughts (data) behind a lock (access control).
➢ Now, let's say your diary is part of a magic backpack. This backpack not only keeps
your diary safe but also has a compartment for your snacks, a pocket for your phone,
and a special slot for your favorite pen. Each compartment does its own thing, and
you can access them when needed. This magic backpack is an encapsulated object.
➢ In programming terms, your diary is like a private variable (hidden data), and the
magic backpack is an object (encapsulation) that holds different things in an
organized way.
➢ So, data hiding is like putting a lock on your secrets, and encapsulation is like having
a magic container that keeps everything tidy and easy to use.
In the world of programming, these concepts – data hiding and encapsulation – help create
more secure, organized, and user-friendly code. They make sure that important information
is protected and that different parts of a program can work together without causing chaos.
It's like having a well-organized party where everyone knows their place, and the treasures
are safely hidden from view.
SECTION-B
3.Write and explain a Program in C++ that uses a constructor and destructor.
Ans: Let's dive into a simple C++ program that uses a constructor and a destructor. I'll break
down each part in simple words to help you understand the basics.
6
Easy2Siksha
Now, let's break it down:
1. Including Necessary Libraries:
7
Easy2Siksha
Here, we include two libraries. The <iostream> library helps us with input and output, and
<string> helps us work with strings.
2. Creating a Class:
We define a class named Book. Inside this class, there are two private variables (title and
author) that will store information about the book.
3. Constructor:
Here, we define a constructor. A constructor is like a special function that is automatically
called when we create a new book. It takes two parameters (the title and the author's
name), and it initializes the title and author variables with these values. It also prints a
message welcoming the new book.
4. Destructor:
8
Easy2Siksha
We define a destructor. A destructor is another special function that is automatically called
when a book is no longer needed. It's like saying goodbye to the book. Here, it prints a
message indicating the book's farewell.
5. Display Details Function:
We have a simple function named displayDetails that shows information about the book,
such as the title and the author.
6. Main Function:
In the main function, we create a book object named tomSawyer using the constructor. This
automatically triggers the constructor, welcoming our new book.
7. Displaying Details:
We display details about the book using the displayDetails function. This will output the title
and author of the book.
9
Easy2Siksha
8. Destructor in Action:
At the end of the main function, when the program is about to finish, the tomSawyer object
goes out of scope. This triggers the destructor, and you'll see a message indicating the
book's farewell.
Understanding the Output: When you run this program, you'll see output messages like:
These messages show when the constructor and destructor are called, welcoming and
saying goodbye to our book.
In essence, the constructor is like a welcome party for a new object, and the destructor is
like a farewell party when the object is no longer needed. They help in managing resources
and performing necessary actions during the lifecycle of an object in C++.
4.How will you overload a constructor in C++?
Ans: Let's break down constructor overloading in C++ in simple words.
Understanding Constructors: In C++, a constructor is like a special function that's
automatically called when you create an object. It's like a welcome party for a new thing
you're making in your program. This party helps set up the new thing with all the necessary
details.
Now, imagine you're making different types of things, and each type needs to be set up in a
specific way. This is where constructor overloading comes in handy.
What is Constructor Overloading? Constructor overloading means having multiple
constructors for a class, each taking a different set of parameters. It's like having different
doors at the entrance of your party, and each door has a different list of things you need to
bring.
10
Easy2Siksha
Let's dive into a simple example to understand this concept better:
11
Easy2Siksha
Breaking it Down:
Class Definition (Car class):
We define a class named Car with private member variables brand, model, and year.
Default Constructor:
The class has a default constructor that gets called when a car is created without any
specific details. It sets default values for brand, model, and year.
12
Easy2Siksha
Parameterized Constructor 1:
= b; model = m; year = y; }
This constructor is for when you know the brand, model, and year of the car. It sets up the
car with the provided details.
Parameterized Constructor 2:
Default year }
This constructor is for when you know the brand and model of the car but not the year. It
sets up the car with the default year.
Function to Display Car Details:
std::endl; } };
13
Easy2Siksha
We have a member function displayDetails() that prints the brand, model, and year of the
car.
Main Function:
In the main function, we create three Car objects using different constructors.
Displaying Details:
We then display the details of each car using the displayDetails() function.
Understanding the Output: When you run this program, you'll see output messages like
14
Easy2Siksha
The output confirms that different constructors are called based on how the cars are
created.
Why is Constructor Overloading Useful?
• Flexibility: Constructor overloading provides flexibility in creating objects. You can
create an object with as much or as little information as you have.
• Convenience: It allows you to initialize objects in various ways, making it convenient
for different use cases.
• Default Values: You can set default values for parameters, making it easier to handle
cases where certain details might be missing.
• Clean Code: It helps in writing clean and readable code, as you can have specific
constructors for different scenarios.
In summary, constructor overloading is like having different doors to enter a party, each
door with its own set of requirements. It allows you to create objects in different ways
based on the information you have, making your code more flexible and adaptable.
SECTION-C
5.Explain the concept of function overloading using C++ code.
Ans: Let's break down the concept of function overloading in C++ in simple words, step by
step.
Introduction to Function Overloading:
In C++, a function is like a mini-program that performs a specific task. Think of it as a named
block of code that you can call by its name. Now, what if you want to perform a similar task
but with different inputs? This is where function overloading comes into play.
The Dilemma:
Imagine you have a magical function called add that adds two numbers. It looks like this:
This works perfectly for adding two integers. But what if you also want to add three integers
or two double numbers? Do you create a new function with a different name for each
scenario? That could get messy.
15
Easy2Siksha
Enter Function Overloading:
• Function overloading is like giving your function a superpower. With function
overloading, you can use the same function name for different versions of the same
task, each tailored to handle specific situations. It's like having multiple flavors of
your favorite ice cream but calling them all "Ice Cream."
How it Works:
Let's dive into a simple example using an imaginary calculator class. This class has an add
function, but now we want it to handle various situations.
Now, we want our Calculator to be smart enough to add three integers as well. Instead of
creating a new function with a different name, we can overload the existing add function.
c; } };
Here, we have two versions of the add function. One adds two integers, and the other adds
three. Now, when you call the add function, the compiler figures out which version to use
based on the number of arguments you provide.
The Magic of Signature:
In C++, the compiler distinguishes between different functions by their signatures. A
signature includes the function name, the type of parameters, and their order. So, even
16
Easy2Siksha
though we have two add functions, the compiler knows which one to use because they have
different signatures.
Adding Doubles to the Mix:
Now, what if you also want your calculator to add doubles? No problem! We can add
another version of the add function that takes double parameters.
Now, you can add integers or doubles using the same function name. The compiler figures
out which version of add to use based on the types of arguments you provide.
Why is it Useful?
1. Readability:
Function overloading makes your code more readable. Instead of having functions with
names like addTwoIntegers, addThreeIntegers, addTwoDoubles, you have a clean and
consistent add function.
2. Logical Organization:
It organizes your code logically. All the variations of a particular task are grouped under the
same function name, making it easier to understand and maintain.
3. No Need to Remember Multiple Names:
You don't have to remember different function names for slightly different tasks. If you
know how to use the add function, you can use it for various scenarios without memorizing
multiple function names.
Example in Action:
Let's use our Calculator class in a simple program to see how it works:
17
Easy2Siksha
In this program, we create an instance of the Calculator class and use its add function in
three different ways. The compiler knows which version of the add function to call based on
the number and types of arguments provided.
Conclusion:
Function overloading is a powerful feature in C++ that simplifies code and enhances
readability. It allows you to use the same function name for different variations of a task,
making your code more organized and easier to maintain. The compiler takes care of
selecting the appropriate function based on the context, making your code more flexible
and adaptable to different scenarios. It's like having a versatile tool in your programming
toolbox, ready to handle various tasks with a single, well-named function.
6.Overload any one unary operator available in C++.
Ans: let's simplify the concept of overloading a unary operator in C++ in simple words.
Introduction to Operators:
In programming, operators are symbols that represent computations or actions to be
performed on variables or values. Unary operators operate on a single operand, which
means they work with only one value.
Basics of Unary Operators:
Unary operators are like little helpers that perform operations on a single value. For
example, the unary minus (-) changes the sign of a number. So, if you have the number 5,
applying the unary minus would make it -5.
18
Easy2Siksha
The Need for Overloading:
In C++, you can create your own classes and define how they behave. Let's say you have a
custom class, and you want to use the unary minus on its objects. By default, the compiler
might not know what it means to negate an object of your class. This is where overloading
comes in.
What is Operator Overloading?
Operator overloading allows you to define how an operator should behave when applied to
objects of a class. It's like telling the compiler what the operator should do in the context of
your class.
Choosing an Operator to Overload:
Let's choose the unary minus (-) operator for our example. We want to define what it means
to negate an object of our custom class.
Example Class - Number:
Let's create a simple class called Number. This class represents a number, and we'll define
how the unary minus should work for objects of this class.
19
Easy2Siksha
Understanding the Code:
Class Definition (Number):
• We have a class named Number with a private member variable value.
• The constructor is used to initialize the value when an object is created.
• We have a getter function getValue to retrieve the value of the Number object.
• Overloading the Unary Minus Operator:
• We overload the unary minus operator (-) within the class.
• The overloaded operator returns a new Number object with the negated value.
Main Function:
• In the main function, we create a Number object num1 with a value of 7.
• We then use the unary minus operator on num1 to get a new Number object called
negatedNum.
• Finally, we display the original and negated values.
How the Unary Minus Operator Works:
When we use the unary minus on a Number object, the overloaded operator- function is
called. It takes the current value of the object, negates it, and creates a new Number object
with the negated value.
Benefits of Operator Overloading:
Intuitive Usage:
With operator overloading, using the unary minus on our custom class feels natural. It's as if
the class understands what it means to be negated.
Code Readability:
The code becomes more readable. Instead of having a separate function like negate(), we
can use the familiar unary minus notation.
20
Easy2Siksha
Consistency:
It provides consistency in the language. If you know how the unary minus works for built-in
types like integers, overloading it for custom classes follows the same logic.
Conclusion:
In simple terms, overloading a unary operator in C++ allows you to define custom behavior
for that operator when applied to objects of your class. It's like giving your class a special
ability to work with operators in a way that makes sense for the context of your program. In
our example, we made the unary minus work seamlessly with our Number class, making it
more versatile and intuitive to use in different scenarios.
SECTION-D
7.What are the different types of inheritances? Explain with C++ code.
Ans: Let's explore the concept of inheritance in C++ in simple words and discuss the
different types of inheritance with corresponding code examples.
Understanding Inheritance:
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a
class to inherit properties and behaviors from another class. This relationship is often
referred to as a "parent-child" or "base-derived" relationship. The class that is being
inherited from is called the "base class" or "parent class," and the class that inherits is called
the "derived class" or "child class."
Types of Inheritance:
1. Single Inheritance:
In single inheritance, a derived class inherits from only one base class. It's like passing down
characteristics from one generation to the next.
Example:
21
Easy2Siksha
In this example, Dog is a derived class that inherits from the Animal base class. The Dog class
has its own method (bark), and it also inherits the eat method from the Animal class.
2. Multiple Inheritance:
In multiple inheritance, a derived class can inherit from more than one base class. It's like
inheriting traits from both your parents.
Example:
22
Easy2Siksha
Here, FlyingFish is a derived class that inherits from both Fish and Bird base classes. It has
access to the swim method from Fish and the fly method from Bird, along with its own
method (display).
3. Hierarchical Inheritance:
In hierarchical inheritance, multiple derived classes inherit from a single base class. It's like
having siblings sharing common characteristics inherited from a parent.
Example:
In this example, both Circle and Square are derived classes inheriting from the Shape base
class. They share the common method draw from the base class and have their own
methods (drawCircle and drawSquare).
23
Easy2Siksha
4. Multilevel Inheritance:
In multilevel inheritance, a derived class becomes the base class for another class. It's like
passing down traits through multiple generations.
Example:
Here, GermanShepherd is a derived class that inherits from the Dog class, which in turn
inherits from the Animal base class. It can access methods from both its immediate parent
(Dog) and the base class (Animal).
Inheritance in Simple Words:
Think of inheritance like a family tree. The base class is like the grandparents, and the
derived classes are like the parents, and so on. Each generation inherits certain
characteristics from the previous one, and new traits can be added along the way.
24
Easy2Siksha
Key Points to Remember:
• Base Class:
The class being inherited from is called the base class or parent class.
• Derived Class:
The class that inherits from another class is called the derived class or child class.
• Inheriting Members:
Derived classes inherit both the attributes (data members) and behaviors (member
functions) of the base class.
• Access Modifiers:
Access specifiers (public, protected, private) define how members of the base class
are accessible in the derived class.
• is-a Relationship:
Inheritance models the "is-a" relationship. For example, a Dog is an Animal.
• Code Reusability:
Inheritance promotes code reusability. You can reuse the code from the base class in
the derived class.
• Polymorphism:
Inheritance is closely related to polymorphism, which allows objects of different
classes to be treated as objects of a common base class.
Conclusion:
Inheritance is a powerful concept in C++ that allows for the creation of hierarchical
relationships between classes. Different types of inheritance (single, multiple, hierarchical,
and multilevel) provide flexibility in modeling real-world scenarios. Understanding these
concepts helps in designing more modular and reusable code, making it easier to manage
and extend as your programs become more complex.
8.What is class template ? Explain using C++.
Ans: Let's explore the concept of class templates in C++ in simple words.
Introduction to Templates:
In C++, templates are a powerful feature that allows you to write generic code. Generic code
means writing code that works with different data types without specifying those types
upfront. This is particularly useful when you want to create flexible and reusable
components.
Understanding Class Templates:
Class templates are a specific type of template in C++. They enable you to define a blueprint
for a class without specifying the data type it will work with. It's like creating a cookie cutter
that can be used to cut out cookies of different shapes without changing the cutter itself.
25
Easy2Siksha
The Need for Class Templates:
Let's consider a scenario where you want to create a container (like an array or list) that can
hold different types of elements. Without templates, you might end up creating separate
classes for each data type (int, double, string, etc.). Class templates provide a more elegant
solution.
Syntax of Class Templates:
The syntax for declaring a class template involves using the template keyword followed by
the template parameter(s) enclosed in angle brackets (<>). Here's a simple example:
Breaking Down the Code:
Class Template Declaration:
template <typename T> indicates the declaration of a class template.
T is a placeholder for the data type. It could be any valid C++ identifier.
Private Member:
The class template has a private member variable element of type T.
26
Easy2Siksha
Constructor:
The constructor takes an argument of type T and initializes the element with that value.
Member Function:
The member function getValue returns the value of the element.
Creating Instances in Main:
Instances of the class template are created with different data types (int, double, and
std::string).
Accessing Values:
Values are accessed and printed using the getValue member function.
Advantages of Class Templates:
1. Code Reusability:
Class templates promote code reusability. You can write a generic class once and use it with
different data types without duplicating the code.
27
Easy2Siksha
2. Flexibility:
They provide flexibility by allowing you to create classes that work with various data types
without modifying the template itself.
3. Cleaner Code:
With class templates, you can write cleaner and more maintainable code since you don't
need to create separate classes for each data type.
4. Generic Algorithms:
Class templates are commonly used in creating generic algorithms and data structures, such
as containers (e.g., vectors, lists) and sorting algorithms.
Common Template Parameters:
1. Single Type Parameter:
A class template can have a single type parameter, as shown in the previous example. This is
suitable for scenarios where you want a class to work with one data type at a time.
2. Multiple Type Parameters:
You can also have multiple type parameters in a class template. This is useful when you
need to work with more than one data type simultaneously.
28
Easy2Siksha
In this example, Pair is a class template that can hold two different types (T and U) in a pair.
Constraints with Templates:
While templates offer flexibility, they come with some considerations:
1. Code Bloat:
Using templates extensively can lead to code bloat, where the compiler generates multiple
copies of the template for different data types. This can increase the size of the compiled
program.
2. Syntax Complexity:
Template syntax might appear complex at first, especially when dealing with more advanced
features. However, for basic use cases, as shown in the examples, the syntax is relatively
straightforward.
Conclusion:
Class templates in C++ provide a powerful mechanism for creating generic classes that can
work with different data types. They promote code reusability and flexibility, allowing you
to write versatile and efficient code. By using class templates, you can design components
that adapt to a variety of data types, making your programs more adaptable and easier to
maintain.
Note: This Answer Paper is totally Solved by Ai (Artificial Intelligence) So if You find Any Error Or Mistake .
Give us a Feedback related Error , We will Definitely Try To solve this Problem Or Error.